package com.cpi.cornpp.corntypes.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.corntypes.dto.CornTypesDTO;
import com.cpi.cornpp.corntypes.exception.CornTypesException;
import com.cs.common.pool.DBDAO;
import com.cs.common.pool.exception.PoolException;
/**
 * 
 * @description: This Class is used to handle the Corn Types Data Access
 * @author Edgar Joao
 * @see DBDAO
 */
public class CornTypesDAO extends DBDAO{
	
	private static final String SELECT_CORN_TYPES = "SELECT corn_type_id, code, description FROM corns_types order by corn_type_id";//$NON-NLS-1$

	public List getListCornTypes() throws CornTypesException {
		List listCornTypesDtos = null;
		PreparedStatement preparedStatement = null;
		CornTypesDTO dto = null;
		ResultSet rs = null;
		try{
			listCornTypesDtos = new ArrayList(0);
			preparedStatement = prepareSQL(SELECT_CORN_TYPES, dsName);
			rs = executeQuery(preparedStatement);
			while (rs.next()){
				dto = new CornTypesDTO();
				dto.setCornTypeId(rs.getInt(CornTypesDTO.CORN_TYPE_ID));
				dto.setCode(rs.getString(CornTypesDTO.CODE));
				dto.setDescription(rs.getString(CornTypesDTO.DESCRIPTION));
				listCornTypesDtos.add(dto);
			}						
		}catch (PoolException poolException){
			CornTypesException cornTypesException = null;
			poolException.printStackTrace(System.out);
			cornTypesException = new CornTypesException(poolException, CornPPException.LAYER_DAO,CornPPException.ACTION_LISTS);
			throw cornTypesException;	
		} catch (SQLException sqlException) {
			CornTypesException cornTypesException = null;
			sqlException.printStackTrace(System.out);
			cornTypesException = new CornTypesException(sqlException, CornPPException.LAYER_DAO, CornPPException.ACTION_LISTS);
			throw cornTypesException;
		} finally {			
			try {				
				closeConnection(preparedStatement.getConnection());				
			} catch (SQLException sqle) {
				sqle.printStackTrace(System.out);
			}
		}
		return listCornTypesDtos;
	}

}
